package com.idle.view;


import java.lang.reflect.Field;

import android.R.attr;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Camera;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.MarginLayoutParams;
import android.view.animation.Transformation;
import android.widget.Gallery;


public class AlignLeftGallery extends Gallery {
	/*public interface IOnItemClickListener {
		public void onItemClick(int position);
	}*/
	private static final String TAG = "AlignLeftGallery";
   /* private Camera mCamera;
    private int mWidth;*/
    private int mPaddingLeft;
    private int mSpacing;
    /*private boolean flag;
    private static int firstChildWidth;
    private static int firstChildPaddingLeft;
    private int offsetX;*/
    
    private OnItemClickListener mListener;
	
    
    public AlignLeftGallery(Context context) {
            super(context);
            //mCamera = new Camera();
            this.setStaticTransformationsEnabled(true);
    }

    public AlignLeftGallery(Context context, AttributeSet attrs) {
            super(context, attrs);
            //mCamera = new Camera();
            setAttributesValue(context, attrs);
            this.setStaticTransformationsEnabled(true);
    }

    public AlignLeftGallery(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            //mCamera = new Camera();
            setAttributesValue(context, attrs);
            this.setStaticTransformationsEnabled(true);
    }

    private void setAttributesValue(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs,
                        new int[] { attr.paddingLeft, attr.spacing });
        mPaddingLeft = typedArray.getDimensionPixelSize(0, 0);
        mSpacing = typedArray.getDimensionPixelSize(1, 0);
        typedArray.recycle();
    }

    protected boolean getChildStaticTransformation(View child, Transformation t) {
        /*t.clear();
        t.setTransformationType(Transformation.TYPE_MATRIX);
        mCamera.save();
        final Matrix imageMatrix = t.getMatrix();
        if (flag) {
                firstChildWidth = getChildAt(0).getWidth();
                Log.i(TAG, "firstChildWidth = " + firstChildWidth);
                firstChildPaddingLeft = getChildAt(0).getPaddingLeft();
                flag = false;
        }
        offsetX = firstChildWidth / 2 + firstChildPaddingLeft + mPaddingLeft
                        - mWidth / 2;
        mCamera.translate(offsetX, 0f, 0f);
        mCamera.getMatrix(imageMatrix);
        mCamera.restore();*/
	
		/*int galleryWidth = this.getWidth();
		int itemWidth = getChildAt(0).getWidth();
		int spacing = mSpacing;
		// We are taking the item widths and spacing from a dimension resource because:
		// 1. No way to get spacing at runtime (no accessor in the Gallery class)
		// 2. There might not yet be any item view created when we are calling this
		// function
	
		// The offset is how much we will pull the gallery to the left in order to simulate
		// left alignment of the first item
		int offset;
		if (galleryWidth <= itemWidth) {
		offset = galleryWidth / 2 - itemWidth / 2 - spacing;
		} else {
		offset = galleryWidth - itemWidth - 2 * spacing;
		}
	
		// Now update the layout parameters of the gallery in order to set the left margin
		MarginLayoutParams mlp = (MarginLayoutParams) this.getLayoutParams();
		mlp.setMargins(-offset, mlp.topMargin, mlp.rightMargin, mlp.bottomMargin);*/
	    return true;
	}

    public void setOnItemClickListener(OnItemClickListener listener) {
    	mListener = listener;
    }
    
    @Override
	public boolean onSingleTapUp(MotionEvent e) {
    	Log.i(TAG, "onSingleTapUp----------------------");
    	try {
			Field f = AlignLeftGallery.class.getSuperclass().getDeclaredField("mDownTouchPosition");
			f.setAccessible(true);
			int position = f.getInt(this);
			Log.i(TAG, "mDownTouchPosition = " + position);
			if(null != mListener && position >= 0) {
				mListener.onItemClick(this, getChildAt(position),
						position, getAdapter().getItemId(position));
			}
		} catch (SecurityException e1) {
			e1.printStackTrace();
		} catch (NoSuchFieldException e1) {
			e1.printStackTrace();
		} catch (IllegalArgumentException e2) {
			e2.printStackTrace();
		} catch (IllegalAccessException e3) {
			e3.printStackTrace();
		} 
		return false;
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		return super.dispatchTouchEvent(ev);
	}

	@Override
    public boolean onTouchEvent(MotionEvent event) {
		Log.i(TAG, "onTouchEvent----------------------");
            //event.offsetLocation(-offsetX, 0);
            return super.onTouchEvent(event);
    }

    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    	Log.i(TAG, "onSizeChanged------- w = " + w + " h = " + h + 
    			"oldw = " + oldw + "oldh = " + oldh);
            /*if (!flag) {
                    mWidth = w;
                    //getLayoutParams().width = mWidth;
                    flag = true;
            }*/
            super.onSizeChanged(w , h, oldw, oldh);
    }
}